﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_ucVendorMaterialsMap : System.Web.UI.UserControl
{
    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnUpdate.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnUpdate.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        if (!IsPostBack)
        {
            GetMaterials();
        }

    }
    protected void btnMove1_Click(object sender, EventArgs e)
    {
        ListItem[] selectedItems = new ListItem[lstAllMaterials.Items.Count];
        int index = 0;

        if (lstAllMaterials.SelectedIndex == -1)
        {            
            return;
        }

        for (int i = 0; i < lstAllMaterials.Items.Count; i++)
            if (lstAllMaterials.Items[i].Selected == true)
            {
                lstSelectedMaterials.Items.Add(new ListItem(lstAllMaterials.Items[i].ToString(), lstAllMaterials.Items[i].Value));
                selectedItems[index] = lstAllMaterials.Items[i];
                index++;
            }
        for (int i = 0; i < index; i++)
            lstAllMaterials.Items.Remove(selectedItems[i]);
    }

    protected void btnMove2_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstAllMaterials.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstSelectedMaterials.Items.Count; j++)
                if (lstAllMaterials.Items[i].Value == lstSelectedMaterials.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstSelectedMaterials.Items.Add(new ListItem(lstAllMaterials.Items[i].Text, lstAllMaterials.Items[i].Value));
            }
        }
        lstAllMaterials.Items.Clear();

    }

    protected void btnMove3_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstSelectedMaterials.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstAllMaterials.Items.Count; j++)
                if (lstSelectedMaterials.Items[i].Value == lstAllMaterials.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstAllMaterials.Items.Add(new ListItem(lstSelectedMaterials.Items[i].Text, lstSelectedMaterials.Items[i].Value));
            }
        }
        lstSelectedMaterials.Items.Clear();
    }

    protected void btnMove4_Click(object sender, EventArgs e)
    {
        ListItem[] selectedItems = new ListItem[lstSelectedMaterials.Items.Count];
        int index = 0;

        if (lstSelectedMaterials.SelectedIndex == -1)
        {
            return;
        }

        for (int i = 0; i < lstSelectedMaterials.Items.Count; i++)
            if (lstSelectedMaterials.Items[i].Selected == true)
            {
                lstAllMaterials.Items.Add(new ListItem(lstSelectedMaterials.Items[i].ToString(), lstSelectedMaterials.Items[i].Value));
                selectedItems[index] = lstSelectedMaterials.Items[i];
                index++;
            }
        for (int i = 0; i < index; i++)
            lstSelectedMaterials.Items.Remove(selectedItems[i]);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "Production/ucVendorList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {      
            AddMaterials();
            if ((int)Session["Product_SelectedVendors"] == 1)
                // remove existing roles while assigning new ones only if one user is selected
                RemoveMaterials();      
    }


    void GetMaterials()
    {
        try
        {
            string condition;
            DataSet dsMaterials = new DataSet();
             
            if (((int)Session["Product_SelectedVendors"]) == 1)
            {
                condition = "T1.VendorID = '" + ((string[])Session["Product_VendorIDs"])[0] + "' and T1.CategoryID = T2.CategoryID";
                dsMaterials = _products.GetData("VendorMaterialMap", "MaterialCategory", condition);
                lstSelectedMaterials.DataSource = dsMaterials;
                lstSelectedMaterials.DataTextField = "CategoryName";
                lstSelectedMaterials.DataValueField = "CategoryID";
                lstSelectedMaterials.DataBind();

                condition = "CategoryID not in (select CategoryID from VendorMaterialMap where VendorID = '" + ((string[])Session["Product_VendorIDs"])[0] + "')";
            }
            else
                condition = "";

            dsMaterials.Clear();
            dsMaterials = _products.GetData("MaterialCategory", condition);
            lstAllMaterials.DataSource = dsMaterials;
            lstAllMaterials.DataTextField = "CategoryName";
            lstAllMaterials.DataValueField = "CategoryID";
            lstAllMaterials.DataBind();
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Get Materials Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void AddMaterials()
    {
        try
        {           
            int count = (int)Session["Product_SelectedVendors"];
            string[] ID = (string[])Session["Product_VendorIDs"];

            if (lstSelectedMaterials.Items.Count == 0)
            {
                Response.Write("<script>alert('No Materials Added')</script>");
                return;
            }

            for (int j = 0; j < count; j++)
                for (int i = 0; i < lstSelectedMaterials.Items.Count; i++)
                {
//                    result = _products.AddMaterials(ID[j], lstSelectedMaterials.Items[i].Value);

                }
           
            Response.Write("<script>alert('Updated')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Assign Role Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void RemoveMaterials()
    {
        try
        {
            string condition;
            int vendors=(int)Session["Product_SelectedVendors"];

            if (lstAllMaterials.Items.Count != 0)
            {

                condition = "VendorID= '" + ((string[])Session["Product_VendorIDs"])[0].ToString() + "' and (CategoryID='" + lstAllMaterials.Items[0].Value + "'";

                for (int i = 1; i < lstAllMaterials.Items.Count; i++)
                {
                    condition = condition + " or CategoryID = " + lstAllMaterials.Items[i].Value + "";
                }
                condition = condition + ")";
//                _products.RemoveMaterials(condition);

                //condition = "(VendorID= '" + ((string[])Session["Product_VendorIDs"])[0].ToString() + "' and (CategoryID='" + lstAllMaterials.Items[0].Value + "'";

                //for (int j = 0; j < vendors; j++)
                //{
                //    if (j != 0) //
                //        condition = condition + "or (VendorID= '" + ((string[])Session["Product_VendorIDs"])[j].ToString() + "' and (CategoryID='" + lstAllMaterials.Items[0].Value + "'";
                //    for (int i = 1; i < lstAllMaterials.Items.Count; i++)
                //    {
                //        condition = condition + " or CategoryID = " + lstAllMaterials.Items[i].Value + "";
                //    }
                //    condition = condition + "))";

                //}
                //_products.RemoveMaterials(condition);
            }
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Remove Materials Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Back()
    {

    }

  
}
